package com.dou.controller;

import com.dou.mapper.LoginDao;
import com.dou.mapper.OrderDao;
import com.dou.mapper.ProductDao;
import com.dou.pojo.order;
import com.dou.pojo.product;
import com.dou.utils.Tools;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA 2021.1 x64
 *
 * @Author: 窦义超
 * @Date: 2021/07/2021/7/29/12:29
 * @Description:
 */
@Controller
public class productController {
    @Autowired
    ProductDao productDao;
    @Autowired
    OrderDao orderDao;
    @Autowired
    LoginDao loginDao;
    @RequestMapping("/product")
    public String product(Model model){
        List<product> allProducts = productDao.getAllProducts();
        model.addAttribute("pros",allProducts);
       model.addAttribute("user", Tools.name);
       model.addAttribute("pro_ids",allProducts.size());
        return "product/product";
    }
    @GetMapping("/toAdd")
    public String toAdd( Model model){
        List<com.dou.pojo.product> allProducts = productDao.getAllProducts();
        model.addAttribute("id",allProducts.size()+1);
        return "product/productAdd";
    }
    @PostMapping("/toAdd")
    public String toAdd2(product product){
        List<com.dou.pojo.product> allProducts = productDao.getAllProducts();
        int count=0;
        for (int i = 0; i <allProducts.size() ; i++) {
            if (product.getPro_id()==allProducts.get(i).getPro_id() ){
                count=count+1;
            }
        }
       if (count==0){
           productDao.addProduct(product);
       }else {
           com.dou.pojo.product productById = productDao.getProductById(product.getPro_id());
           product.setPro_count(productById.getPro_count()+product.getPro_count());
           productDao.updatePro(product);
           List<order> order1 = orderDao.selectAllByID(Tools.user_id);
           int count1=0;
           for (int i = 0; i < order1.size(); i++) {
               if(order1.get(i).getOrder_id()==product.getPro_id()){
                   count++;
               }
           }
           if(count1>0){
               order order = new order();
               order.setOrder_id(product.getPro_id());
               order.setOrder_name(product.getPro_name());
               order.setUser_id(Tools.user_id);
               order.setOrder_count(orderDao.selectOne(order).getOrder_count());
               orderDao.updateOrder(order);
           }
       }

        return "redirect:/product";
    }


    @RequestMapping("/buy")
    public String buy(product product){
int count=0;
int count1=0;
        com.dou.pojo.product productSelect = productDao.getProductById(product.getPro_id());
        count1=productSelect.getPro_count();
        productSelect.setPro_count(productSelect.getPro_count()-product.getPro_count());

        List<order> orders = orderDao.selectAllByID(Tools.user_id);
        for (int i = 0; i < orders.size(); i++) {
            if(orders.get(i).getOrder_id()==productSelect.getPro_id()){
                count++;
            }
        }
        if(product.getPro_count()> count1){
            product.setPro_count(count1);
            productSelect.setPro_count(0);
        }
        productDao.updatePro(productSelect);
        if( count==0){
            order o1=new order();
            o1.setOrder_name(productSelect.getPro_name());
            o1.setOrder_count(product.getPro_count());
            o1.setOrder_id(product.getPro_id());
            o1.setUser_id(Tools.user_id);
           orderDao.addOrder(o1);
       }else{
            order o1=new order();
            o1.setOrder_id(product.getPro_id());
            o1.setUser_id(Tools.user_id);
            order order = orderDao.selectOne(o1);
            order o2=new order();
            o2.setUser_id(Tools.user_id);
            o2.setOrder_count(order.getOrder_count()+ product.getPro_count());
            o2.setOrder_id(productSelect.getPro_id());
            orderDao.updateOrder(o2);
        }
        return "redirect:/product";
    }
}
